#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


int socket_init(u_int16_t port,u_int32_t server_ip,struct sockaddr_in *addr)
{
	struct in_addr a;
	a.s_addr=server_ip;
	printf("the server ip is %s\n",inet_ntoa(a));	
	int s;
	s=socket(AF_INET,SOCK_DGRAM,0);
	//next is init the sockaddr_in of server
	
	/* 填写sockaddr_in*/
	bzero(addr,sizeof(struct sockaddr_in));

	addr->sin_family = AF_INET;
	addr->sin_port = htons(port);
	addr->sin_addr.s_addr = server_ip;

	return s;
}


int main()
{

//	u_int16_t port=4000;
//	char *server_ip="192.168.77.132";

	u_int16_t port=3000;
	char *server_ip="127.0.0.1";

	struct sockaddr_in addr;
	
	struct sockaddr_in addr_client;

	int s,len=128;
	int i=0;	
	int addr_len =sizeof(struct sockaddr_in);
	char buffer[2000];

	s=socket_init(port,inet_addr(server_ip),&addr);

	bzero(buffer,sizeof(buffer));

        sendto(s,"nidaye",7,0,(struct sockaddr *)&addr,addr_len);	

	while(1){
		printf("have sended\n");
		/* 接收server端返回的字符串*/
		len = recvfrom(s,buffer,sizeof(buffer),0,(struct sockaddr *)&addr,&addr_len);
		printf("receive:%d\n",sizeof(buffer));
	
		for(i=0;i<16;i++)
		{
			printf("0x%x",buffer[i]);
		}
		printf("             ");
		for(i=16;i<28;i++)
		{
			printf("0x%x",buffer[i]);
		}
		printf("              ");
		for(i=28;i<36;i++)
		{
			printf("0x%x",buffer[i]);
		}
		printf("\n");
			
	}
}
